import os, random
import Warehouse
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
class FinishError (Exception):
    pass
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def OrderSpawn (orders: list, player_level: int, customer_name: list, customer_need: dict):
    if (len (orders)):
        for i in range (len (orders)):
            if (orders[i][5] > 1):
                orders[i][5] -= 1
            elif (orders[i][5] == 1):
                print (f"{orders[i][0]}的{orders[i][1]}需求订单已超时！")
                del orders[i]
    goods = [# A城-五姨菜市场
             ["菠菜", 4, "斤"], ["茼蒿", 4, "斤"], ["油菜", 4, "斤"], ["生菜", 4, "斤"],
             ["大蒜", 5, "斤"], ["大葱", 5, "斤"], ["番茄", 5, "斤"], ["土豆", 5, "斤"],
             ["黄瓜", 6, "斤"], ["洋葱", 6, "斤"],
             # A城-二狗肉市
             ["鸡肉", 8, "斤"], ["鸭肉", 9, "斤"], ["猪肉", 10, "斤"], ["羊肉", 15, "斤"],
             ["牛肉", 18, "斤"], ["鲫鱼", 20, "斤"], ["河虾", 35, "斤"], ["河蟹", 50, "斤"],
             ["鲍鱼", 90, "斤"], ["龙虾", 120, "斤"],
             # B城-三叔五金行
             ["5号螺帽", 6, "包"], ["5号螺丝", 6, "包"], ["电池", 8, "个"], ["十字螺丝刀", 13, "个"],
             ["一字螺丝刀", 13, "个"], ["扳手", 30, "个"], ["锤子", 60, "个"], ["电子秤", 70, "个"],
             ["电钻", 100, "个"], ["全能工具箱", 188, "箱"],
             # B城-四姐药铺
             ["OK绷", 2, "个"], ["绷带", 7, "卷"], ["碘液", 12, "瓶"], ["75%酒精", 15, "瓶"],
             ["333感冒灵", 15, "盒"], ["花香正气水", 20, "盒"], ["布落芬", 22, "盒"], ["蒙脱石莎", 30, "盒"],
             ["急速救心丸", 40, "盒"], ["医疗套装包", 78, "包"],
             # C城-六兄布行
             ["纺织针", 3, "包"], ["剪刀", 10, "个"], ["卷尺", 10, "条"], ["棉布", 18, "包"],
             ["五彩逢衣线", 20, "盒"], ["丝绸", 27, "m²"], ["麻布", 40, "m²"], ["皮革", 50, "m²"],
             ["编制套装包", 150, "包"], ["缝纫机", 500, "个"],
             # C城-大舅家具城
             ["小凳子", 50, "个"], ["椅子", 70, "个"], ["转椅", 119, "个"], ["矮桌", 199, "个"],
             ["床头柜", 219, "个"], ["木桌", 359, "个"], ["马桶", 488, "个"], ["衣柜", 799, "个"],
             ["沙发", 1699, "个"], ["洗衣机", 1899, "个"],
             # D城-十子杂货铺
             ["卫生纸", 5, "卷"], ["铅笔", 5, "盒"], ["橡皮", 5, "个"], ["可乐", 5, "瓶"],
             ["雪碧", 5, "瓶"], ["方便面", 8, "桶"], ["尺子", 9, "套"], ["薯片", 12, "包"],
             ["扫把", 35, "把"], ["拖把", 50, "把"],
             # D城-八婶水果店
             ["黄梨", 10, "斤"], ["菠萝", 10, "斤"], ["苹果", 11, "斤"], ["香蕉", 12, "斤"],
             ["蓝莓", 18, "斤"], ["葡萄", 20, "斤"], ["草莓", 25, "斤"], ["杨桃", 45, "斤"],
             ["西瓜", 53, "个"], ["榴莲", 80, "个"],
             # E城-七哥电子城
             ["充电线", 20, "个"], ["充电器", 35, "个"], ["插排", 55, "个"], ["鼠标", 88, "个"],
             ["键盘", 120, "个"], ["耳机", 150, "个"], ["音响", 199, "个"], ["屏幕", 259, "个"],
             ["GPU", 1500, "块"], ["CPU", 2100, "块"],
             # E城-四叔建材坊
             ["玻璃", 22, "m²"], ["陶瓷", 25, "m²"], ["石膏板", 228, "块"], ["保温板", 488, "块"],
             ["防腐木材", 450, "根"], ["水泥", 490, "袋"], ["高端木材", 650, "根"], ["防水涂料", 1290, "桶"],
             ["红砖", 3600, "千块"], ["钢筋", 4880, "吨"]] # 格式如下["名字", 单价, "单位"]
    
    if (player_level <= 10):
        spawn = random.randint (1, 40 - player_level)
    elif (player_level > 10):
        spawn = random.randint (1, 25 - player_level)
    
    if (spawn == 3):
        order_goods = random.sample (goods, 1)
        order_customer = random.sample (customer_name, 1)
        amount = random.randint[10, 50]
        for i in range (1, 4):
            if (order_goods == customer_need[order_customer + str (i)]):
                s = 1
            else :
                s = 0
        if (s):
            orders.append ([order_customer, order_goods[0], amount, order_goods[2], int (amount * order_goods[1] * 1.7), 5])
        else :
            orders.append ([order_customer, order_goods[0], amount, order_goods[2], int (amount * order_goods[1] * 1.3), 5])
        print ("你有新的订单！")
    return orders
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def OrderMenu (save, orders, player_warehouse_list, player_warehouse_list_name: list, player_xp, player_level, money, warehouse_level, acc_num, times_orders):
    # orders 格式如下：["用户", "商品", 单位数量, "单位", 总价, 时效]
    os.system ('cls')
    
    while (1):
        num = 0
        succees_orders = []
        try:
            print ("|---------------------------------------")
            print ("|当前订单数量：")
            if (not len (orders)):
                print ("|暂无订单")
            else :
                num = 1
                for i in orders:
                    print (f"|\033[39;32m{num}. \033[0m{i[0]} 需要“{i[1]}”{i[2]}{i[3]}，共计[{i[4]}₿，距离订单截止剩余{i[5]}天 ", end = '')
                    if (i[0] in player_warehouse_list_name):
                        if (i[2] >= player_warehouse_list[player_warehouse_list_name.index (i[0])][1]):
                            print (f"|\033[39;32m{num}. \033[0m{i[0]} 需要“{i[1]}”{i[2]}{i[3]}，共计[{i[4]}₿，距离订单截止剩余{i[5]}天 ", end = '')
                            print ("\033[1;39;32m当前订单可完成\033[0m")
                            succees_orders.append (num)
                        else :
                            print (f"|{num}. {i[0]} 需要“{i[1]}”{i[2]}{i[3]}，共计[{i[4]}₿，距离订单截止剩余{i[5]}天 ", end = '')
                            print ("\033[1;38;5;208m仓库内数量不足\033[0m")
                    else :
                        print (f"|{num}. {i[0]} 需要“{i[1]}”{i[2]}{i[3]}，共计[{i[4]}₿，距离订单截止剩余{i[5]}天 ", end = '')
                        print ("\033[1;39;31m仓库内无该商品\033[0m")
                    num += 1
            print ("|---------------------------------------")
            print (f"|已完成{times_orders}次订单")
            print ("|---------------------------------------")
            print ("|x. 输入有色序号以完成订单")
            print ("|Vw. 查看仓库")
            print ("|0. 返回")
            print ("|---------------------------------------")
            chos = input ("输入需要的功能：")
            if (chos == 'Vw'):
                list_amount = 0
                os.system ('cls')
                for i in player_warehouse_list:
                    list_amount += i[1]
                Warehouse.WarehouseAllGoods (player_warehouse_list, list_amount, warehouse_level)
            elif (int (chos) in succees_orders):
                one_order = orders[int (chos) - 1]
                player_warehouse_list, player_warehouse_list_name, player_xp, player_level, money, acc_num, times_orders = ReachedOrder (one_order, player_warehouse_list, player_warehouse_list_name, player_xp, player_level, money, acc_num, times_orders)
                del orders[int (chos) - 1]
                save = 0
            elif (int (chos) == 0):
                return save, orders, player_warehouse_list, player_warehouse_list_name, player_xp, player_level, money, acc_num, times_orders
            elif (num - 1 >= int (chos) > 0):
                raise FinishError
            else :
                raise ValueError
        except ValueError:
            os.system ('cls')
            print ("\033[1;31m无效选项！\033[0m\n")
        except FinishError:
            os.system ('cls')
            print ("\033[1;31m该订单无法完成！\033[0m\n")
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def ReachedOrder (one_order, player_warehouse_list, player_warehouse_list_name: list, player_xp, player_level, money, acc_num, times_orders):
    x = player_warehouse_list_name.index (one_order[0])
    player_warehouse_list[x][1] -= one_order[2]
    money += one_order[4]
    player_xp += int (one_order[2] / 2)
    if (player_warehouse_list[x][1] == 0):
        del player_warehouse_list_name[x], player_warehouse_list[x]
    print (f"已完成{i[0]}的“{i[1]}”{i[2]}{i[3]}订单！")
    print (f"获得{int (one_order[2] / 2)}点经验\n")
    level_up_need_xp = [10, 20, 40, 80, 160,
                        260, 350, 500, 650, 800,
                        950, 980, 1000, 1000, 1000,
                        1000, 1000, 1000, 1000, 0]
    while (True):
            i = float (level_up_need_xp[player_level - 1]) / 40.0
            x = int (player_xp / i)
            level_up = 0
            if (player_xp >= level_up_need_xp[player_level - 1]):
                player_xp -= level_up_need_xp[player_level - 1]
                player_level += 1
                level_up += 1
            else :
                if (level_up):
                    print ("LEVEL UP!!!")
                    print (f"已提升{level_up}级！")
                print (f"当前{player_level}")
                for i in range (x):
                    print ("\033[1;32m━", end = "")
                for i in range (40 - x):
                    print ("\033[1;37m━", end = "")
                print ("\033[0m")
                print (f"距离下一级还需{level_up_need_xp[player_level - 1] - player_xp}点经验")
                break
    times_orders += 1
    if ((times_orders >= 100) and (2 not in acc_num)):
        acc_num.append (2)
        print ("\n\033[33m达成成就《\033[38;5;219m百单王\033[33m》！\033[0m")
    input ("按Enter继续...")
    return player_warehouse_list, player_warehouse_list_name, player_xp, player_level, money, acc_num, times_orders